package com.cskaoyan.servlet;


import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminKeywordService;
import com.cskaoyan.service.AdminKeywordServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

// 关键词
@WebServlet("/admin/keyword/*")
public class AdminKeywordServlet extends WdBaseServlet {

    AdminKeywordService adminKeywordService = new AdminKeywordServiceImpl();

    // 展示market_keyword 表的内容
    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        // 1. 请求参数的接收
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");


        // 关键字，跳转链接  keyword,url
        String keyword = request.getParameter("keyword");
        String url = request.getParameter("url");

        CommonData data = adminKeywordService.query(page, limit, sort, order, keyword, url);

        return BaseRespVo.ok(data);
    }

    // 添加功能
    public BaseRespVo create(HttpServletRequest request, HttpServletResponse response) throws IOException {

        System.out.println("添加功能");
        // 格式化时间
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 注意月和小时的格式为两个大写字母
        Date updateTime = new Date();// 获得当前时间
        // 处理json函数
        String jsonStr = request.getReader().readLine();
        MarketKeyword marketKeyword = JacksonUtil.read(jsonStr, MarketKeyword.class);// 获取到的就是更新以后的

        marketKeyword.setUpdateTime(updateTime);
        marketKeyword.setAddTime(updateTime);

        adminKeywordService.insert(marketKeyword);


        return BaseRespVo.ok(marketKeyword);

    }

    // 编辑更新操作
    @SneakyThrows
    public BaseRespVo update(HttpServletRequest request, HttpServletResponse response) {
        // 时间
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date updateTime = new Date();

        String jsonStr = request.getReader().readLine();
        MarketKeyword marketKeyword = JacksonUtil.read(jsonStr, MarketKeyword.class);

        marketKeyword.setUpdateTime(updateTime);

        // 更新数据
        adminKeywordService.update(marketKeyword);
        return BaseRespVo.ok(marketKeyword);
    }

    // 删除操作
    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) throws IOException {

        // 获取要删除的
        String jsonStr = request.getReader().readLine();
        MarketKeyword marketKeyword = JacksonUtil.read(jsonStr, MarketKeyword.class);

        int i = adminKeywordService.delete(marketKeyword.getId());
        System.out.println("i = " + i);
        return BaseRespVo.ok();


    }


}
